package com.example.sunnyweather.ui.place

import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.addTextChangedListener

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.sunnyweather.databinding.FragmentPlaceBinding

class PlaceFragment : androidx.fragment.app.Fragment() {

    val viewModel by lazy { ViewModelProvider(this).get(PlaceViewModel::class.java) }

    private lateinit var  adapter:PlaceAdapter

    private lateinit var binding:FragmentPlaceBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPlaceBinding.inflate(inflater,container,false)
//        return inflater.inflate(R.layout.fragment_place,container,false)
        val root:View = binding.root
        return root


    }




    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
//        val layoutManager = LinearLayoutManager(activity)
//        binding.recyclerView.layoutManager = layoutManager
//        adapter = PlaceAdapter(this,viewModel.placeList)
//        binding.recyclerView.adapter = adapter
//        binding.searchPlaceEdit.addTextChangedListener{
//            editable ->
//            val content = editable.toString()
//            if(content.isNotEmpty()){
//                viewModel.searchPlaces(content)
//            }else{
//                binding.recyclerView.visibility = View.GONE
//                binding.bgImageView.visibility = View.VISIBLE
//                viewModel.placeList.clear()
//                adapter.notifyDataSetChanged()
//            }
//        }
//        viewModel.placeLiveData.observe(viewLifecycleOwner, Observer{
//            result ->
//            val places = result.getOrNull()
//            if(places != null){
//                binding.recyclerView.visibility = View.VISIBLE
//                binding.bgImageView.visibility = View.GONE
//                viewModel.placeList.clear()
//                viewModel.placeList.addAll(places)
//                adapter.notifyDataSetChanged()
//            }else{
//                Toast.makeText(activity,"未能查询到任何地点",Toast.LENGTH_SHORT).show()
//                result.exceptionOrNull()?.printStackTrace()
//            }
//        })
    }
}